package com.schoolET.service.wechat.wechatImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.config.OssConfig;
import com.schoolET.entity.*;
import com.schoolET.entity.dto.GoodsDTO;
import com.schoolET.entity.dto.GoodsInfoDTO;
import com.schoolET.entity.dto.GoodsTypeInfoDTO;
import com.schoolET.entity.dto.WechatInfoDTO;
import com.schoolET.entity.vo.AddGoodsVO;
import com.schoolET.error.costom.*;
import com.schoolET.mapper.*;
import com.schoolET.service.wechat.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: lin
 * @className: GoodsServiceImpl
 * @createTime: 2024-06-06  10:45
 * @description:
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Resource
    private OssConfig ossConfig;


    @Resource
    private OrderManagementMapper orderManagementMapper;

    @Resource
    private WechatMapper wechatMapper;

    @Resource
    private RegionMapper regionMapper;

    @Resource
    private WechatAndRegionMapper wechatAndRegionMapper;

    @Override
    public GoodsInfoDTO getGoodsById(Integer id) {
        GoodsInfoDTO goodsInfoDTO = baseMapper.selectGoodsById(id);
        if (goodsInfoDTO == null) {
            throw new NoGoodsException();
        }
        return goodsInfoDTO;
    }

    @Override
    public GoodsType getGoodsTypeById(Integer id) {
        return goodsTypeMapper.selectById(id);
    }

    @Override
    @Transactional
    public boolean addGoods(AddGoodsVO addGoodsVO, String openId) {
        WechatInfoDTO wechatInfoDTO = wechatMapper.selectByOpenId(openId);
        WechatAndRegion wechatAndRegion = wechatAndRegionMapper.selectByWechatId(wechatInfoDTO.getId());
        Goods goods = new Goods(wechatInfoDTO.getId(),addGoodsVO.getFileImgUrl(),addGoodsVO.getTheme(),addGoodsVO.getDescribe(),addGoodsVO.getPrice(),addGoodsVO.getAddress());
        baseMapper.insert(goods);
        baseMapper.insertGoodsAndRegion(goods.getId(), wechatAndRegion.getRegionId());
        GoodsType goodsType = goodsTypeMapper.selectById(addGoodsVO.getTypeId());
        if (goodsType == null) {
            throw new NoGoodsTypeException();
        }
        Integer goodsId = goods.getId();
        baseMapper.insertGoodAndType(goodsId, addGoodsVO.getTypeId());
        OrderManagement orderManagement = new OrderManagement();
        orderManagement.setGoodsId(goodsId);
        orderManagement.setReleaseId(wechatInfoDTO.getId());
        orderManagementMapper.insert(orderManagement);

        return true;
    }

    @Override
    public Page<Goods> getMyPurchaseGoods(String openId, Integer pageNum, Integer pageSize) {
        WechatInfoDTO wechatInfoDTO = wechatMapper.selectByOpenId(openId);
        if (wechatInfoDTO == null) {
            throw new NoWechatException();
        }
        Page<Goods> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectMyPurchaseGoods(page, wechatInfoDTO.getId());
    }

    @Override
    public Page<Goods> getMySellGoods(String openId, Integer pageNum, Integer pageSize) {
        WechatInfoDTO wechatInfoDTO = wechatMapper.selectByOpenId(openId);
        if (wechatInfoDTO == null) {
            throw new NoWechatException();
        }
        Page<Goods> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectMySellGoods(page, wechatInfoDTO.getId());
    }

    @Override
    public boolean takenShelves(Integer goodsId, String openId) {
        WechatInfoDTO wechatInfoDTO = wechatMapper.selectByOpenId(openId);
        if (wechatInfoDTO == null) {
            throw new NoWechatException();
        }
        Integer i = orderManagementMapper.updateOrderManagementByGoodsIdAndReleaseId(goodsId, wechatInfoDTO.getId(), new Date());
        if (i != 1) {
            throw new GoodsEditException();
        }
        return true;
    }

    @Override
    public List<GoodsTypeInfoDTO> getGoodsTypeInfo() {
        List<GoodsType> goodsTypes = goodsTypeMapper.selectList(new QueryWrapper<GoodsType>());
        log.info("goodsTypes:{}", goodsTypes);
        return BeanUtil.copyToList(goodsTypes, GoodsTypeInfoDTO.class);
    }

    @Override
    public Page<Goods> getAllGoodsByTypeAndTheme(String openId,
                                                 Integer goodsTypeId,
                                                 String theme,
                                                 Integer pageNum,
                                                 Integer pageSize) {
        WechatInfoDTO wechatInfoDTO = wechatMapper.selectByOpenId(openId);
        if (wechatInfoDTO == null) {
            throw new NoWechatException();
        }
        Region region = regionMapper.selectRegionByWechatId(wechatInfoDTO.getId());
        Page<Goods> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectByGoodsTypeIdAndTheme(page, goodsTypeId, theme, region.getId(),wechatInfoDTO.getId());
    }
    @Override
    public List<GoodsDTO> getGoodsDTOPage(Integer pageNum, Integer pageSize) {
        return baseMapper.selectGoodsDTOPage((pageNum-1)*pageSize,pageSize);
    }

    @Override
    public String putImage(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty() || file.getSize() == 0) {
            throw new NoPhotoException();
        }
        return ossConfig.putObject(file,"goods");
    }
}
